package com.bw.a2207a.arouter

import android.content.Context
import android.text.TextUtils
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.bw.a2207a.Const

/**
 *
 * @ClassName: LoginIntercepter
 * @Description: 登录拦截器
 * @Author: zhangyanchao
 * @CreateDate: 2024/10/21 3:07 PM
 * @Version: 1.0
 */
@Interceptor(priority = 1, name = "LoginInterceptor")
class LoginInterceptor : IInterceptor {
    override fun init(context: Context?) {
    }

    override fun process(postcard: Postcard, callback: InterceptorCallback?) {
        //该拦截器会在每次路由跳转前检查用户是
        //否已经登录。如果用户没有登录，拦截器会阻止路由请求，并跳转到登录页面
        val token = SPUtils.getInstance().getString("token")
//        if (TextUtils.isEmpty(token) && !postcard.path.equals(Const.path_login)) {
        if (TextUtils.isEmpty(token)) {
            //    /main/MainActivity
            //未登录，且，跳转目标页面不是登录页面，拦截跳转，执行登录页面跳转
            ARouter.getInstance()
                .build(Const.path_login)
                .greenChannel()//绿色渠道，不执行拦截器
                .navigation()

            callback?.onInterrupt(null)
        } else {
            //不拦截，继续路由跳转
            callback?.onContinue(postcard)
        }
    }
}